﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Db4objects.Db4o;
using Db4objects.Db4o.Linq;
using System.Linq;

namespace Yosca
{
	/// <summary>
	/// Interaction logic for ReportControl.xaml
	/// </summary>
	public partial class ReportControl : UserControl
	{
		public ReportControl()
		{
			this.InitializeComponent();
		}

		private void isCreditDebitChecked(object sender, System.Windows.RoutedEventArgs e)
		{
            expenseFlags ef = expenseFlags.Instance;
            ef.flags[0] = true;
		}

		private void isCashChecked(object sender, System.Windows.RoutedEventArgs e)
		{
            expenseFlags ef = expenseFlags.Instance;
            ef.flags[2] = true;
		}

		private void isCheckChecked(object sender, System.Windows.RoutedEventArgs e)
		{
            expenseFlags ef = expenseFlags.Instance;
            ef.flags[1] = true;
		}
		
		private void isCreditDebitUnchecked(object sender, System.Windows.RoutedEventArgs e)
		{
        	expenseFlags ef = expenseFlags.Instance;
            ef.flags[0] = false;
		}

		private void isCashUnchecked(object sender, System.Windows.RoutedEventArgs e)
		{
        	expenseFlags ef = expenseFlags.Instance;
            ef.flags[2] = false;
		}

		private void isCheckUnchecked(object sender, System.Windows.RoutedEventArgs e)
		{
        	expenseFlags ef = expenseFlags.Instance;
            ef.flags[1] = false;
		}

		private void ExpenseReportClick(object sender, System.Windows.RoutedEventArgs e)
		{
            try
            {
                //these lines will fail if either is not set, pretty cheap
                expStartDate.SelectedDate.GetType();
                expEndDate.SelectedDate.GetType();

                reportGrid.Columns.Clear();
                reportGrid.AutoGenerateColumns = false;
                populateReportGrid();
                
                expenseFlags ef = expenseFlags.Instance;
                IObjectContainer expDB = Db4oFactory.OpenFile("expDB.ass");
                IEnumerable<Expense> eList = from Expense E in expDB
                                             where E.purchaseDate.Date >= expStartDate.SelectedDate.Value.Date && E.purchaseDate <= expEndDate.SelectedDate.Value.Date
                                             where (E.flags[0] == ef.flags[0] && ef.flags[0] == true) || (E.flags[1] == ef.flags[1] && ef.flags[1] == true) || (E.flags[2] == ef.flags[2] && ef.flags[2] == true)
                                             select E;
                reportGrid.ItemsSource = eList.ToList<Expense>();
                expDB.Close();
            }
            catch
            {
                MessageBox.Show("ERROR: Please enter both a start and end date");
            }

		}
		
		public void populateReportGrid()
        {
            //expenseGrid.AutoGenerateColumns = false;
            DataGridTextColumn dateCol = new DataGridTextColumn();
            dateCol.Header = "Purchase Date";
            dateCol.Binding = new Binding("purchaseDate");
            dateCol.Binding.StringFormat = "MMMM dd, yyyy";
            reportGrid.Columns.Add(dateCol);

            DataGridTextColumn amountCol = new DataGridTextColumn();
            amountCol.Header = "Amount Paid";
            amountCol.Binding = new Binding("amount");
            amountCol.Binding.StringFormat = "$0.00";
            reportGrid.Columns.Add(amountCol);

            DataGridTextColumn payeeCol = new DataGridTextColumn();
            payeeCol.Header = "Paid to";
            payeeCol.Binding = new Binding("whoToPay");
            reportGrid.Columns.Add(payeeCol);

            DataGridTextColumn descCol = new DataGridTextColumn();
            descCol.Header = "Description";
            descCol.Binding = new Binding("description");
            reportGrid.Columns.Add(descCol);

            DataGridTextColumn payMethodCol = new DataGridTextColumn();
            payMethodCol.Header = "Payment Method";
            payMethodCol.Binding = new Binding("payMethod");
            reportGrid.Columns.Add(payMethodCol);

            reportGrid.CanUserAddRows = false;
            IObjectContainer expDB = Db4oFactory.OpenFile("expDB.ass");
            IEnumerable<Expense> EList = from Expense E in expDB
                                         select E;
            if(EList.ToList() != null)
                reportGrid.ItemsSource = EList.ToList<Expense>();
            expDB.Close();
        }

		private void byThinkChecked(object sender, System.Windows.RoutedEventArgs e)
		{
            InvoiceFlags ivf = InvoiceFlags.Instance;
            ivf.flags[2] = true;
		}

		private void byTalkChecked(object sender, System.Windows.RoutedEventArgs e)
		{
            InvoiceFlags ivf = InvoiceFlags.Instance;
            ivf.flags[4] = true;
		}

		private void byOtherChecked(object sender, System.Windows.RoutedEventArgs e)
		{
            InvoiceFlags ivf = InvoiceFlags.Instance;
            ivf.flags[1] = true;
		}

		private void byHighChecked(object sender, System.Windows.RoutedEventArgs e)
		{
            InvoiceFlags ivf = InvoiceFlags.Instance;
            ivf.flags[3] = true;
		}

		private void byDriveChecked(object sender, System.Windows.RoutedEventArgs e)
		{
            InvoiceFlags ivf = InvoiceFlags.Instance;
            ivf.flags[0] = true;
		}

		private void viewInvRange(object sender, System.Windows.RoutedEventArgs e)
		{
			reportGrid.Columns.Clear();
			reportGrid.CanUserAddRows = false;
			reportGrid.AutoGenerateColumns = false;
			populateVGWInv(reportGrid);
        	Database InvDB = Database.Instance;
            InvoiceFlags invF = InvoiceFlags.Instance;
            try
            {
                IEnumerable<Invoice> invList = from Invoice inv in InvDB.db
                                               where inv.scheduledDate.Date >= invStartDate.SelectedDate.Value.Date && inv.scheduledDate.Date <= invEndDate.SelectedDate.Value.Date
                                               where (inv.flags[0] == invF.flags[0] && invF.flags[0] == true) || (inv.flags[1] == invF.flags[1] && invF.flags[1] == true) || (inv.flags[2] == invF.flags[2] && invF.flags[2] == true) || (inv.flags[3] == invF.flags[3] && invF.flags[3] == true) || (inv.flags[4] == invF.flags[4] && invF.flags[4] == true)
                                               select inv;

                reportGrid.ItemsSource = invList.ToList<Invoice>();
            }
            catch
            {
                MessageBox.Show("ERROR: Please enter both a start and end date");
            }
		}
		
		public void populateVGWInv(DataGrid gridley)
		{
			DataGridTextColumn nameCol = new DataGridTextColumn();
            nameCol.Header = "Name";
            nameCol.Binding = new Binding("defCust.name");
            gridley.Columns.Add(nameCol);
		}

		private void viewGenReport(object sender, System.Windows.RoutedEventArgs e)
		{
            Database invDB = Database.Instance;
           
            IList<Invoice> invList = invDB.db.Query<Invoice>(delegate(Invoice invoice)
            {
                 return invoice.scheduledDate != null && invoice.scheduledDate.Year == genStartDate.SelectedDate.Value.Year && invoice.scheduledDate.Month == genStartDate.SelectedDate.Value.Month;
            });
            reportGrid.ItemsSource = invList.ToList<Invoice>();
		}

		private void byThinkUnchecked(object sender, System.Windows.RoutedEventArgs e)
		{
            InvoiceFlags ivf = InvoiceFlags.Instance;
            ivf.flags[2] = false;
		}

		private void byTalkUnchecked(object sender, System.Windows.RoutedEventArgs e)
		{
            InvoiceFlags ivf = InvoiceFlags.Instance;
            ivf.flags[4] = false;
		}

		private void byOtherUnchecked(object sender, System.Windows.RoutedEventArgs e)
		{
            InvoiceFlags ivf = InvoiceFlags.Instance;
            ivf.flags[1] = false;
		}

		private void byHighUnchecked(object sender, System.Windows.RoutedEventArgs e)
		{
            InvoiceFlags ivf = InvoiceFlags.Instance;
            ivf.flags[3] = false;
		}

		private void byDriveUnchecked(object sender, System.Windows.RoutedEventArgs e)
		{
            InvoiceFlags ivf = InvoiceFlags.Instance;
            ivf.flags[0] = false;
		}


	}
}